<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Book Detail Page</title>
</head>
<body>
<h4>Book Detail Page</h4>
Book:<%= request.getParameter("book")%>
<br/><br/>
<a href="books.jsp">返回</a>

<%
    String book = request.getParameter("book");

    Cookie[] cookies = request.getCookies();
    List<Cookie> cookieList = new ArrayList<Cookie>();
    Cookie tempCookies = null;
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            //以...开头
            if (cookieName.startsWith("YJH")) {
                cookieList.add(cookie);
                if (cookie.getValue().equals(book)) {
                    tempCookies = cookie;
                }
            }
        }
    }

    if (cookieList.size() >= 5 && tempCookies == null) {
        tempCookies = cookieList.get(0);
    }

    if (tempCookies!=null){
        tempCookies.setMaxAge(0);
        response.addCookie(tempCookies);
    }

    Cookie cookie = new Cookie("YJH" + book, book);
    response.addCookie(cookie);
%>
</body>
</html>
